package org.example.spirngmvcdemo.controller;

import org.example.spirngmvcdemo.model.Message;
import org.example.spirngmvcdemo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    private List<Message> messageList = new ArrayList<>();

    // 发布留言
    @RequestMapping("/publish")
    public boolean publish(Message message) {
        if(!StringUtils.hasLength(message.getFrom())
                ||!StringUtils.hasLength(message.getTo())
                ||!StringUtils.hasLength(message.getMessage())) {
            return false;
        }
//        messageList.add(message);
        messageService.insert(message);
        return true;
    }

    // 获取留言列表
    @RequestMapping("/getList")
    public List<Message> getList() {
        return messageService.queryMessage();
//        return messageList;
    }

}
